package by.yakusekmaxim.bookgid.webapp.page.admin.image;

import java.util.Iterator;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

import by.yakusekmaxim.bookgid.datamodel.Image;
import by.yakusekmaxim.bookgid.datamodel.Image_;
import by.yakusekmaxim.bookgid.services.ImageService;

public class ImageListPanel extends Panel {

	@Inject
	private ImageService imageService;


	public ImageListPanel(String id) {
		super(id);
		ImageDataProvider imageDataProvider = new ImageDataProvider();

		DataView<Image> dataView = new DataView<Image>("list",
				imageDataProvider, 10) {
			@Override
			protected void populateItem(Item<Image> item) {
				final Image image = item.getModelObject();
				item.add(new Label("id"));
				item.add(new Label("name"));
				Label imageLabel = new Label("image","");
				imageLabel
				.add(AttributeModifier.replace("src", image.getUrl()));
				item.add(imageLabel);
				
				item.add(new Link<Object>("edit") {
					@Override
					public void onClick() {
						setResponsePage(new EditImagePage(image));
					}
				});
				item.add(new Link<Object>("delete") {
					@Override
					public void onClick() {
						imageService.delete(image);
						setResponsePage(new ImagePage());
					}
				});
			}

		};

		add(dataView);

		add(new PagingNavigator("paging", dataView));

		add(new OrderByBorder<SingularAttribute<Image, ?>>("sortByID",
				Image_.id, imageDataProvider));
		add(new OrderByBorder<SingularAttribute<Image, ?>>("sortByName",
				Image_.name, imageDataProvider));

	}

	private class ImageDataProvider extends
			SortableDataProvider<Image, SingularAttribute<Image, ?>> {

		public ImageDataProvider() {
			super();
			setSort(Image_.id, SortOrder.ASCENDING);

		}

		@Override
		public Iterator<? extends Image> iterator(long first, long count) {

			SingularAttribute<Image, ?> sortParam = getSort().getProperty();
			SortOrder propertySortOrder = getSortState().getPropertySortOrder(
					sortParam);
			boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);

			return imageService.getAllImages(sortParam, ascending,
					(int) first, (int) count).iterator();
		}

		@Override
		public long size() {

			return imageService.getCount();
		}

		@Override
		public IModel<Image> model(Image image) {
			return new CompoundPropertyModel<Image>(image);
		}
	}
}
